<template>
  <div class="workshop-search flex">
    <select-tree label="所在位置" :sel-tree="queryParams.location" class="margin-r-20"
                 ref="selectTree"
                 :tree-list="workshopOpt" node-key="uniqueKey" @treeChange="_changeTreeNode"/>
    <select-tooltip
      v-model="queryParams.devices"
      isShowAll
      :options="deviceOptions"
      msg="加工设备"
    ></select-tooltip>
    <select-tooltip
      v-if="isShift"
      v-model="queryParams.shift"
      isShowAll
      :multiple="false"
      :options="shiftOptions"
      msg="加工班次"
    ></select-tooltip>
  </div>
</template>

<script>
import { getOrganize } from '../../../../boot/status-monitor/machine'
import { defaultSchemesClearer } from '../../../../assets/js/util'
import { getShiftsList } from '../../../../boot/job/man-machine-bind'
import SelectTree from 'src/components/select-tree'
import _ from 'lodash'
import { getDeviceIds } from '../../../../boot/statistics/device-rate'
import { getOrganizeNestedNew } from '../../../../boot/factory/organize'
export default {
  name: 'workshop-search',
  components: {
    SelectTree
  },
  props: {
    value: [Array, String, Number, Date, Object],
    isShift: {
      type: Boolean,
      default: true
    },
    urlId: Number,
    isFirstDevices: {
      type: Boolean,
      default: true
    }
  },
  data () {
    return {
      queryParams: {
        location: {
          id: null,
          level: null,
          label: '',
          hash: ''
        },
        devices: ['all'],
        shift: 'all',
        workshopId: ''
      },
      workshopId: '', // 车间id
      workshopOpt: [], // 车间
      deviceOptions: [], // 设备组
      shiftOptions: [] // 班次
    }
  },
  watch: {
    value: {
      handler (val) {
        this.queryParams = val
      },
      deep: true,
      immediate: true
    },
    queryParams: {
      deep: true,
      handler () {
        this.$emit('input', this.queryParams)
      }
    }
  },
  computed: {
  },
  methods: {
    // 赋值 + 重置 两种效果
    // 默认重置
    initData (isInit = true, params) {
      if (isInit) {
        this.queryParams.devices = ['all']
        this.queryParams.shift = 'all'
        this.getWorkshops()
      } else {
        this.getWorkshops(params)
      }
    },
    // 获取所在位置树结构
    getWorkshops (params) {
      // 获取组织机构树
      const url = this.urlId ? getOrganizeNestedNew(this.urlId) : getOrganize()
      return url.then(res => {
        // 去除 default 节点
        let clearTree = defaultSchemesClearer.clear(res.data)

        const reorganize = (node) => {
          // 为节点添加唯一键，用于 tree 组件标识唯一节点
          node.uniqueKey = node.level + ':' + node.id

          if (node.children instanceof Array) {
            node.children.forEach(reorganize)
          }
        }
        // 重新组织 tree
        clearTree.forEach(reorganize)
        this.workshopOpt = clearTree
        if (!_.isEmpty(params)) {
          const member = this.workshopOpt.find(v => v.level === 2 && v.id === params.workshopId)
          this.$refs.selectTree.setValue(member)
          this._changeTreeNode(member)
          this.$emit('onReady')
        } else {
          this.$refs.selectTree.setValue(clearTree[0])
          this._changeTreeNode(clearTree[0])
        }
      })
    },
    _changeTreeNode (val) {
      if (val.id && val.label && val.uniqueKey) {
        this.$set(this.queryParams.location, 'id', val.id)
        this.$set(this.queryParams.location, 'level', val.level)
        this.$set(this.queryParams.location, 'label', val.label)
        this.$set(this.queryParams.location, 'hash', val.hash)
        // 获取设备
        this.queryParams.workshopId = val.level === 2 ? val.id : val.workshopId
        this.getDevices(val)
      } else {
        this.$set(this.queryParams.location, 'id', null)
        this.$set(this.queryParams.location, 'level', null)
        this.$set(this.queryParams.location, 'label', '')
        this.$set(this.queryParams.location, 'hash', '')
        this.deviceOptions = []
        this.queryParams.deviceIds = ['all']
      }
      this.isShift && this.getShift()
    },
    getDevices (val) {
      this.deviceOptions = []
      getDeviceIds(val.id, val.level).then(res => {
        this.allDevicesList = res.data.list
        this.deviceOptions = res.data.list.map(item => {
          return {
            value: item.id,
            assetNumber: item.assetNumber,
            label: `${item.name}(${item.assetNumber})`
          }
        })
        // this.deviceOptions = this.$utils.sortByAny(this.deviceOptions, 'assetNumber')
        // 默认选择全部设备
        this.queryParams.devices = ['all']
        this.isFirstDevices && this.$emit('endDevice', this.deviceOptions)
      })
    },
    getShift () {
      this.queryParams.shift = 'all'
      this.shiftOptions = []
      if (this.queryParams.workshopId) {
        const params = {
          id: this.queryParams.workshopId
        }
        getShiftsList(params).then(res => {
          this.shiftOptions = res.data.map(item => {
            return {
              label: item.name,
              value: item.id
            }
          })
        })
      }
    }
  },
  mounted () {
  }
}
</script>

<style scoped lang="scss"></style>
